package com.jwthhealth_pub.wristband.steps.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.signutil.db.DbHelper;
import com.jwthhealth_pub.wristband.steps.bean.StepsDayModel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每天的运动
 * Created by yang on 2017/12/25.
 */

public class StepsDayDao {
    private static StepsDayDao instance;
    private Context context;
    private Dao<StepsDayModel, Integer> stepsDayDao;
    private DbHelper databaseHelper;
    private StepsDayModel dataBean;

    @SuppressWarnings("unchecked")
    public StepsDayDao(Context context) {
        super();
        this.context = context;

        try {
            databaseHelper = DbHelper.getHelper(context);
            stepsDayDao = databaseHelper.getDao(StepsDayModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static StepsDayDao getInstance() {

        if (instance == null) {
            instance = new StepsDayDao(App.mContext);
        }

        return instance;
    }

    /**
     * 插入一条数据
     * @param
     * @return
     */
    public boolean add(StepsDayModel stepsDayModel){
        boolean isInsert = false;
        try {
            int result = stepsDayDao.create(stepsDayModel);
            if (result > 0)
                isInsert = true;
        } catch (SQLException e) {
            Log.e("StepsDayDao", e.toString());
        }
        return isInsert;
    }
    /*
  * 根据日期查询有没有相同的  false没有 true有  在执行add()
  */
    public boolean  queryByDateEqual(String day_date) {
        Boolean isEqual =false;
        try {
            List<StepsDayModel> dataBeens = stepsDayDao.queryForEq("day_date",day_date);
            if (dataBeens.size() > 0) {
                isEqual =true;
            } else {
                isEqual =false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isEqual;
    }

    /*
    * 根据日期查询
    */
    public StepsDayModel queryByDateStepsDayModel(String day_date) {
        try {
            List<StepsDayModel> dataBeens = stepsDayDao.queryForEq("day_date",day_date);
            if (dataBeens.size() > 0) {
                dataBean = dataBeens.get(0);
            } else {
                dataBean = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataBean;
    }

    /*
    * 根据日期前7天查询
    */
    public  List<StepsDayModel> queryByWeekData(String day_date) {
        List<StepsDayModel> dataBeens = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        try {
            //过去七天
            c.setTime(new Date());
            c.add(Calendar.DATE, - 7);
            Date d = c.getTime();
            long ts = d.getTime();
             String  res = String.valueOf(ts);
             dataBeens = stepsDayDao.queryBuilder().where().between("day_date", res, day_date).query();// 获取day_date是1到day_date之间的结果
            if (dataBeens.size() > 0) {
                return dataBeens;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataBeens;
    }
    /*
    * 根据日期前1月查询
    */
    public  List<StepsDayModel> queryByMonthData(String day_date) {
        List<StepsDayModel> dataBeens = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(new Date());
            c.add(Calendar.MONTH, -1);
            Date m = c.getTime();
            long ts = m.getTime();
            String  res = String.valueOf(ts);
            dataBeens = stepsDayDao.queryBuilder().where().between("day_date", res, day_date).query();// 获取day_date是1到day_date之间的结果
            if (dataBeens.size() > 0) {
                return dataBeens;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataBeens;
    }

    /*
   * 根据日期前1年查询
   */
    public  List<StepsDayModel> queryByYearData(String day_date) {
        List<StepsDayModel> dataBeens = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(new Date());
            c.add(Calendar.YEAR, -1);
            Date m = c.getTime();
            long ts = m.getTime();
            String  res = String.valueOf(ts);
            dataBeens = stepsDayDao.queryBuilder().where().between("day_date", res, day_date).query();// 获取day_date是1到day_date之间的结果
            if (dataBeens.size() > 0){
                return dataBeens;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataBeens;
    }



}
